<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';
echo $action;
switch ($action) {
	
	case 'addDept' :
		addDept();
		break;
		
	case 'modifyDept' :
		modifyDept();
		break;
		
	case 'deleteDept' :
		deleteDept();
		break;   

	default :
	    // if action is not defined or unknown
		// move to main Dept page
		header('Location: index.php');
}


function addDept()
{
	$id   			= (int)$_GET['id'];
	$qg_id			= (int)$_POST['cboCountry'];
	$truong_id		= (int)$_POST['cboSchool'];
    $madv    		= $_POST['txtMadv'];
	$tendv 			= $_POST['txtTendv'];
	$dienthoai_dv  	= $_POST['txtDienthoai_dv'];
	$diadiem	  	= $_POST['txtDiadiem'];
	$ngaythanhlap   = $_POST['txtNgaythanhlap'];
	
	$sql2   = "INSERT INTO donvi(madv, tendv, dienthoai_dv, diadiem, ngaythanhlap, truong_id)
	          VALUES ('$madv', '$tendv', '$dienthoai_dv', '$diadiem', '$ngaythanhlap', $truong_id)";
		

	$result2 = dbQuery($sql2);

	header("Location: index.php");	
}

//	Modify a Dept

function modifyDept()
{
	$id   			= (int)$_GET['id'];
	$truong_id		= (int)$_POST['cboSchool'];
    $madv    		= $_POST['txtMadv'];
	$tendv 			= $_POST['txtTendv'];
	$dienthoai_dv  	= $_POST['txtDienthoai_dv'];
	$diadiem	  	= $_POST['txtDiadiem'];
	$ngaythanhlap   = $_POST['txtNgaythanhlap'];
	
	$sql2   = "UPDATE donvi
               SET tendv = '$tendv', dienthoai_dv = '$dienthoai_dv', diadiem = '$diadiem', ngaythanhlap = '$ngaythanhlap', truong_id = $truong_id
			   WHERE id = $id" ;		
	echo $sql2;   
	//exit();
	$result2 = dbQuery($sql2);
	echo $sql2;
	
	header('Location: index.php');			  
}

//Delete Dept
function deleteDept()
{
	if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
	} else {
		header('Location:index.php');
	}	
		
	$sql1 = "UPDATE donvi SET hieu_luc = 0 WHERE id = $id"; 

	dbQuery($sql1);
		
    header('Location: index.php'); 
}

?>